package com.czxy.service;


import com.czxy.dao.ManagerMapper;
import com.czxy.domain.Manager;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class ManagerServiceImpl implements  ManagerService {

    @Resource
    private ManagerMapper managerMapper;




    @Override
    public Manager login(Manager manager) {

        //拼查询条件
        Example example = new Example(Manager.class);
        Example.Criteria  ct = example.createCriteria();
        ct.andEqualTo("username",manager.getUsername());
        ct.andEqualTo("password",manager.getPassword());

        List<Manager> list = managerMapper.selectByExample(example);

        if (list.size()>0){

            return list.get(0);
        }else{

            return null;

        }

    }

    @Override
    public PageInfo findByInfo(Integer pageNum, Integer pageSize, Manager manager) {

        Example example = new Example(Manager.class);
        Example.Criteria ct = example.createCriteria();

        if (manager.getUsername() != null && !"".equals(manager.getUsername())){
            ct.andLike("username",manager.getUsername()+"%");
        }



        if (manager.getSex() != null && !"".equals(manager.getSex())){
            ct.andEqualTo("sex",manager.getSex());
        }

        if (manager.getType() != null && !"".equals(manager.getType())){
            ct.andEqualTo("type",manager.getType());
        }


        PageHelper.startPage(pageNum,pageSize);


        List<Manager> list = managerMapper.selectByExample(example);

        PageInfo<Manager> pageInfo = new PageInfo<>(list);


        return pageInfo;
    }
























}
